﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CodeGeneratorHelper.Entities
{
    public class ColumnInfo
    {
        public string Name { get; set; }
        public DBColumnType Type { get; set; }
        public int Length { get; set; }
        public int Numeric_Precision { get; set; }
        public int Numeric_Scale { get; set; }
        public string GetDBParamStr()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(UserSettings.ParamStr).Append(Name).Append(" ");
            switch (Type)
            {
                case DBColumnType.DateTime:
                    sb.Append(" [datetime] ");
                    break;
                case DBColumnType.Date:
                    sb.Append(" [date] ");
                    break;
                case DBColumnType.UniqIdentifier:
                    sb.Append(" [uniqueidentifier] ");
                    break;
                case DBColumnType.Decimal:
                    sb.Append(" [decimal] (").Append(Numeric_Precision).Append(",").Append(Numeric_Scale).Append(") ");
                    break;
                case DBColumnType.Int:
                    sb.Append(" int ");
                    break;
                case DBColumnType.Numeric:
                    sb.Append(" [numeric] (").Append(Numeric_Precision).Append(",").Append(Numeric_Scale).Append(") ");
                    break;
                case DBColumnType.VarChar:
                    sb.Append(" varchar(").Append(Length).Append(")");
                    break;
                case DBColumnType.Boolean:
                    sb.Append(" bit ");
                    break;
            }
            sb.Append(" = null ,");
            return sb.ToString();
        }
        public string GetReaderConversionString()
        {
            StringBuilder sb = new StringBuilder();
            switch (Type)
            {
                case DBColumnType.DateTime:
                    sb.Append(" Convert.ToDateTime(objReader[\"").Append(Name).Append("\"]) : DateTime.Now");
                    break;
                case DBColumnType.Date:
                    sb.Append(" Convert.ToDateTime(objReader[\"").Append(Name).Append("\"]) : DateTime.Now");
                    break;
                case DBColumnType.Int:
                    sb.Append(" Convert.ToInt32(objReader[\"").Append(Name).Append("\"]) : 0");
                    break;
                case DBColumnType.Decimal:
                case DBColumnType.Numeric:
                    sb.Append(" Convert.ToDecimal(objReader[\"").Append(Name).Append("\"]) : 0");
                    break;
                case DBColumnType.VarChar:
                    sb.Append(" objReader[\"").Append(Name).Append("\"].ToString() : \"\"");
                    break;
                case DBColumnType.Boolean:
                    sb.Append(" Convert.ToBoolean(objReader[\"").Append(Name).Append("\"]) : false");
                    break;
            }
            
            return sb.ToString();
        }
        public string GetECNReaderConversionString()
        {
            StringBuilder sb = new StringBuilder();
            switch (Type)
            {
                case DBColumnType.DateTime:
                    sb.Append(" ToSqlDateTime(reader[\"").Append(Name).Append("\"])");
                    break;
                case DBColumnType.Date:
                    sb.Append(" ToSqlDate(reader[\"").Append(Name).Append("\"])");
                    break;
                case DBColumnType.Int:
                    sb.Append(" ToSqlInt32(reader[\"").Append(Name).Append("\"])");
                    break;
                case DBColumnType.Decimal:
                case DBColumnType.Numeric:
                    sb.Append(" ToSqlDouble(reader[\"").Append(Name).Append("\"])");
                    break;
                case DBColumnType.VarChar:
                    sb.Append(" ToString(reader[\"").Append(Name).Append("\"])");
                    break;
                case DBColumnType.Boolean:
                    sb.Append(" ToSqlBoolean(reader[\"").Append(Name).Append("\"])");
                    break;
                case DBColumnType.UniqIdentifier:
                    sb.Append(" ToSqlGuid(reader[\"").Append(Name).Append("\"])");
                    break;
            }

            return sb.ToString();
        }
    }
    public enum DBColumnType
    {
        VarChar,
        Int,
        Decimal,
        DateTime,
        Date,
        Numeric,
        Boolean,
        UniqIdentifier,

    }
}
